home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / knowhow4 / addevent.cpp < prev    next >
C/C++ Source or Header  |  1994-10-10  |  9KB  |  316 lines

  1. #include "addevent.h"
  2. #include <dos.h>
  3. #include <ctype.h>
  4.  
  5. #define MACROS_STACK 20      // Stack for calling the macros from another macros
  6.  
  7. char* scriptFileName = NULL; // Name of the current script
  8. int scriptMode = GO;         // Go mode - no script processing
  9. FILE* scriptPtr = NULL;      // Pointer in the script file
  10. char* macrosFileName = NULL;
  11. FILE* macrosPtr = NULL;
  12. macros_context mac_table[MAX_MACROSES];           // table for macros searching
  13.  
  14. int num_com = 0;               // 5 commands per line - return ...
  15.  
  16. mac_status macros_stack[MACROS_STACK];    // stack for calling macros from another macros
  17. int macros_used = 0;                   // No of mac_status(es) in stack
  18.  
  19. struct var           // Variable name have corresponding code
  20.     {
  21.     char* command;
  22.     int code;
  23.     };
  24.  
  25. var commands[] =
  26.     {
  27.     { "ESC", EVENT_ESC },                   { "TAB", EVENT_TAB },
  28.     { "BKSP", EVENT_BKSP },                { "RETURN", EVENT_RETURN },
  29.     { "LEFT", EVENT_LEFT },                { "RIGHT", EVENT_RIGHT },
  30.     { "HOME", EVENT_HOME },                { "UP", EVENT_UP },
  31.     {"PG_UP", EVENT_PG_UP },               { "END", EVENT_END },
  32.     { "DN", EVENT_DN },                    { "PG_DN", EVENT_PG_DN },
  33.     { "CTRL_HOME", EVENT_CTRL_HOME },      { "PG_UP", EVENT_CTRL_PG_UP },
  34.     { "CTRL_END", EVENT_CTRL_END },        { "PG_DN", EVENT_CTRL_PG_DN },
  35.     { "F1", EVENT_F1 },                    { "F2", EVENT_F2 },
  36.     { "F3", EVENT_F3 },                    { "F4", EVENT_F4 },
  37.     { "F5", EVENT_F5 },                    { "F6", EVENT_F6 },
  38.     { "F7", EVENT_F7 },                    { "F8", EVENT_F8 },
  39.     { "F9", EVENT_F9 },                    { "F10", EVENT_F10 },
  40.     { "SHIFT_F1", EVENT_SHIFT_F1 },        { "SHIFT_F2", EVENT_SHIFT_F2 },
  41.     { "SHIFT_F3", EVENT_SHIFT_F3 },        { "SHIFT_F4", EVENT_SHIFT_F4 },
  42.     { "SHIFT_F5", EVENT_SHIFT_F5 },        { "SHIFT_F6", EVENT_SHIFT_F6 },
  43.     { "SHIFT_F7", EVENT_SHIFT_F7 },        { "SHIFT_F8", EVENT_SHIFT_F8 },
  44.     { "SHIFT_F9", EVENT_SHIFT_F9 },        { "SHIFT_F10", EVENT_SHIFT_F10 },
  45.     { "CTRL_F1", EVENT_CTRL_F1 },          { "CTRL_F2", EVENT_CTRL_F2 },
  46.     { "CTRL_F3", EVENT_CTRL_F3 },          { "CTRL_F4", EVENT_CTRL_F4 },
  47.     { "CTRL_F5", EVENT_CTRL_F5 },          { "CTRL_F6", EVENT_CTRL_F6 },
  48.     { "CTRL_F7", EVENT_CTRL_F7 },          { "CTRL_F8", EVENT_CTRL_F8 },
  49.     { "CTRL_F9", EVENT_CTRL_F9 },          { "CTRL_F10", EVENT_CTRL_F10 },
  50.     { "ALT_F1", EVENT_ALT_F1 },            { "ALT_F2", EVENT_ALT_F2 },
  51.     { "ALT_F3", EVENT_ALT_F3 },            { "ALT_F4", EVENT_ALT_F4 },
  52.     { "ALT_F5", EVENT_ALT_F5 },            { "ALT_F6", EVENT_ALT_F6 },
  53.     { "ALT_F7", EVENT_ALT_F7 },            { "ALT_F8", EVENT_ALT_F8 },
  54.     { "ALT_F9", EVENT_ALT_F9 },            { "ALT_F10", EVENT_ALT_F10 },
  55.     { "ALT_A", EVENT_ALT_A },              { "ALT_B", EVENT_ALT_B },
  56.     { "ALT_C", EVENT_ALT_C },              { "ALT_D", EVENT_ALT_D },
  57.     { "ALT_E", EVENT_ALT_E },              { "ALT_F", EVENT_ALT_F },
  58.     { "ALT_G", EVENT_ALT_G },              { "ALT_H", EVENT_ALT_H },
  59.     { "ALT_I", EVENT_ALT_I },              { "ALT_J", EVENT_ALT_J },
  60.     { "ALT_K", EVENT_ALT_K },              { "ALT_L", EVENT_ALT_L },
  61.     { "ALT_M", EVENT_ALT_M },              { "ALT_N", EVENT_ALT_N },
  62.     { "ALT_O", EVENT_ALT_O },              { "ALT_P", EVENT_ALT_P },
  63.     { "ALT_Q", EVENT_ALT_Q },              { "ALT_R", EVENT_ALT_R },
  64.     { "ALT_S", EVENT_ALT_S },              { "ALT_T", EVENT_ALT_T },
  65.     { "ALT_U", EVENT_ALT_U },              { "ALT_V", EVENT_ALT_V },
  66.     { "ALT_W", EVENT_ALT_W },              { "ALT_X", EVENT_ALT_X },
  67.     { "ALT_Y", EVENT_ALT_Y },              { "ALT_Z", EVENT_ALT_Z },
  68.  
  69.     { "ALT_1", EVENT_ALT_1 },              { "ALT_2", EVENT_ALT_2 },
  70.     { "ALT_3", EVENT_ALT_3 },              { "ALT_4", EVENT_ALT_4 },
  71.     { "ALT_5", EVENT_ALT_5 },              { "ALT_6", EVENT_ALT_6 },
  72.     { "ALT_7", EVENT_ALT_7 },              { "ALT_8", EVENT_ALT_8 },
  73.     { "ALT_9", EVENT_ALT_9 },              { "ALT_10", EVENT_ALT_0 },
  74.     { "ALT_MINUS", EVENT_ALT_MINUS },      { "ALT_EQ", EVENT_ALT_EQUAL },
  75.     { "ALT_TAB", EVENT_ALT_TAB },
  76.     { "CTRL_Y", EVENT_CTRL_Y },
  77.     { "INS", EVENT_INS },                  { "DEL", EVENT_DEL },
  78.     { "SHIFT_TAB", EVENT_SHIFT_TAB },
  79.     { "", 0 }
  80.     };
  81.  
  82. /////////////////////////
  83. int read_command(char* command, FILE* f)      // call after '<' char was obtained
  84.     {
  85.     memset(command, '\0', 20);        // set str to ['\0', '\0', '\0', '\0'...]
  86.     int i = 0;
  87.     char ch;
  88.     while((ch = fgetc(f)) != '>'     // while the '>' - obtain
  89.      && !isspace(ch) && i < 20)          // command
  90.     {
  91.     command[i++] = ch;
  92.     }
  93.     i = 0;
  94.     while(commands[i].code)                        // search for this command
  95.     {                                          // in commands[] array
  96.     if(strcmp(commands[i].command, command))   // if not find - check next
  97.         {
  98.         i++;
  99.         continue;
  100.         }
  101.     return commands[i].code;
  102.     }
  103.     return 0;
  104.     }
  105. /////////////////////////
  106. void init_macros()
  107.     {
  108.     macrosFileName = "macros.mac";
  109.     macrosPtr = fopen(macrosFileName, "r");
  110.  
  111.     rewind(macrosPtr);
  112.  
  113.     char str[22];
  114.     int table_pos = 0;
  115.     while(table_pos < MAX_MACROSES)
  116.     {
  117.     fpos_t reserv_pos;
  118.     fgetpos(macrosPtr, &reserv_pos);
  119.  
  120.     if(fgets(str, 20, macrosPtr) == NULL)
  121.         break;
  122.     if(str[0] != '$')           // $ <ALT_F1>
  123.         continue;            // ... macros body
  124.  
  125.     fseek(macrosPtr, reserv_pos + 3, SEEK_SET);   // skip " <" after $
  126.     int key;
  127.     if(key = read_command(str, macrosPtr))        // for example "ALT_F1"
  128.         {
  129.         fgetpos(macrosPtr, &reserv_pos);
  130.         mac_table[table_pos].key = key;
  131.         mac_table[table_pos].pos = reserv_pos;
  132.         table_pos++;
  133.         continue;
  134.         }
  135.     }
  136.     }
  137. ////////////////////////
  138. void process_event(int mode)
  139.     {
  140.     if(mode == 1)  // simple mode
  141.     {
  142.     e = getevent(KEYEVENT | MOUSEEVENT, MCleftDn | MCrightDn);
  143.     return;
  144.     }
  145.     else if(mode == 2)  // simple mode
  146.     {
  147.     e = getevent(KEYEVENT | MOUSEEVENT, MCleftDn | MCrightDn | MCmove);
  148.     return;
  149.     }
  150.  
  151.     while(1)
  152.     {
  153.     e = readevent(KEYEVENT | MOUSEEVENT);
  154.  
  155.     if((e.what == MOUSEEVENT || e.what == NOEVENT)
  156.         && e.msstatus.buttonstate != 0)
  157.         {
  158.         e.what = MOUSEEVENT;
  159.         break;
  160.         }
  161.     if(e.what == KEYEVENT)
  162.         {
  163.         break;
  164.         }
  165.     }
  166.     }
  167. //////////////////////
  168. void get_command(char* c)
  169.     {
  170.     if(e.is_char())
  171.     {
  172.     c[1] = '\0';
  173.     c[0] = e.key;
  174.     return;
  175.     }
  176.     int i = 0;
  177.     while(e.key != commands[i].code)
  178.     i++;
  179.  
  180.     c[0] = ' ';
  181.     c[1] = '<';
  182.  
  183.     strcpy(c + 2, commands[i].command);
  184.     int l = strlen(commands[i].command);
  185.     c[l + 2] = '>';
  186.     c[l + 3] = ' ';
  187.     c[l + 4] = '\0';
  188.     }
  189. /////////////////////////
  190. void command_get()
  191.     {
  192.     int ch;
  193.     while((ch = fgetc(scriptPtr)) == ' ' || ch == '\n')
  194.     ;
  195.     if(ch == EOF)
  196.     {
  197.     e.what = NOEVENT;
  198.     e.key = 0;
  199.     return;
  200.     }
  201.  
  202.     if(ch != '<')        // char
  203.     {
  204.     e.key = ch;
  205.     return;
  206.     }
  207.  
  208.     char command[20];
  209.     e.key = read_command(command, scriptPtr);
  210.     }
  211. //////////////////////
  212. mac_status macros_pop()
  213.     {
  214.     macros_used--;
  215.     return macros_stack[macros_used];
  216.     }
  217. //////////////////////
  218. void macros_push(mac_status mac)
  219.     {
  220.     if(macros_used < MACROS_STACK)
  221.     {
  222.     macros_stack[macros_used++] = mac;
  223.     }
  224.     }
  225. //////////////////////
  226. void find_command()
  227.     {
  228.     fpos_t reserv_pos;
  229.     fgetpos(scriptPtr, &reserv_pos);
  230.  
  231.     int table_pos = 0;
  232.     while(table_pos < MAX_MACROSES && mac_table[table_pos].key)
  233.     {
  234.     if(e.key == mac_table[table_pos].key)
  235.         {
  236.         mac_status mac;              // remember old file executed
  237.         mac.file = scriptFileName
  238.         ? strdup(scriptFileName) : NULL;
  239.         mac.ptr = scriptPtr;
  240.         mac.mode = scriptMode;
  241.         mac.pos = reserv_pos;
  242.  
  243.         macros_push(mac);                  // and keep it in the stack
  244.  
  245.         delete scriptFileName;
  246.         scriptFileName = macrosFileName
  247.         ? strdup(macrosFileName) : 0;
  248.         scriptPtr = macrosPtr;
  249.         scriptMode = PLAY;
  250.         fseek(scriptPtr, mac_table[table_pos].pos, SEEK_SET);
  251.         break;
  252.         }
  253.     table_pos++;
  254.     }
  255.     }
  256. //////////////////////
  257. void check_macros()
  258.     {
  259.     if(e.is_char())
  260.     return;
  261.     find_command();           // try to find the macros and play it
  262.     }
  263. //////////////////////
  264. void get_event(int mode)
  265.     {
  266.     switch(scriptMode)
  267.     {
  268.     case GO:
  269.         process_event(mode);
  270.         check_macros();
  271.         break;
  272.     case RECORD:
  273.         process_event(mode);
  274.  
  275.         char command[22];
  276.         get_command(command);
  277.         fputs(command, scriptPtr);
  278.         num_com++;
  279.         if(num_com > 5)
  280.         {
  281.         char nl[2];
  282.         nl[0] = '\n';
  283.         nl[1] = '\0';
  284.         fputs(nl, scriptPtr);
  285.         num_com = 0;
  286.         }
  287.  
  288.         check_macros();
  289.         break;
  290.     case PLAY:
  291.         e.what = KEYEVENT;
  292.         command_get();
  293.         check_macros();
  294.         if(e.key == 0 || e.key == '$') // or beginning of the next macros
  295.         {
  296.         delete scriptFileName;
  297.         scriptFileName = NULL;
  298.         mac_status mac = macros_pop();         // obtain previous status
  299.         if(macros_used == 0)
  300.             {
  301.             scriptMode = GO;
  302.             delete mac.file;
  303.             break;
  304.             }
  305.  
  306.         scriptMode = mac.mode;          // and restore it
  307.  
  308.         scriptFileName = mac.file ? strdup(mac.file) : 0;
  309.         delete mac.file;
  310.         scriptPtr = mac.ptr;
  311.         fseek(scriptPtr, mac.pos, 0);
  312.         }
  313.         break;
  314.     }
  315.     }
  316.